package lambda;


import org.junit.Test;

import java.util.Comparator;

/**
 * @description: Lambda表达式测试
 * @author: apple
 * @date:  2022/1/1 10:59 下午
 * @version 1.0
 **/
public class LambdaTest {

    @Test
    public void test1() {
        // 使用匿名内部类来实现
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("hello world");
            }
        };

        runnable.run();

        // 使用Lambda表达式实现
        Runnable runnable1 = () -> {
            System.out.println("hello world");
            int i = 0;
            if (true){
                System.out.println("aaa");
            }
            for (int j = 0; j < 10; j++) {
                System.out.println(j+1);
            }
        };

        runnable1.run();
    }

    @Test
    public void test2() {

        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };

        final int compare = com1.compare(10, 12);
        System.out.println(compare);

        System.out.println("************");

        // Lambda表达式写法
        Comparator<Integer> com2 = (o1,o2) -> Integer.compare(o1, o2);
        final int compare1 = com2.compare(10, 12);
        System.out.println(compare1);

        // 方法引用写法
        Comparator<Integer> com3 = Integer :: compare;
        final int compare2 = com3.compare(10, 12);
        System.out.println(compare2);
    }
}
